#define _GNU_SOURCE

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>


void my_abort(void){
    static int count;
    struct sigaction act;

    printf("Get SIGABRT count %d\n", count);
    count++;
    if (count > 1){
        if(sigaction(SIGABRT, NULL, &act) ==-1){
           printf("Error get sigaction SIGABRT %s\n", strerror(errno)); 
           exit(EXIT_FAILURE);
        }
        act.sa_handler = SIG_DFL;
        if(sigaction(SIGABRT, &act, NULL) ==-1){
           printf("Error set sigaction SIGABRT %s\n", strerror(errno)); 
           exit(EXIT_FAILURE);
        }
        puts("SIGABRT set SIG_DFL");       
    }
    if(raise(SIGABRT)!=0){
        printf("Error send SIGABRT %s\n", strerror(errno));
    }
}

void sigHandlerAbrt(int sig){
    printf("Get signal %d\n", sig);
    my_abort();
}


int main(){
    struct sigaction act;
    act.sa_handler = &sigHandlerAbrt;
    if(sigaction(SIGABRT, &act, NULL) ==-1){
        printf("Error sigaction SIGABRT %s\n", strerror(errno)); 
        exit(EXIT_FAILURE);        
    }
    printf("Pid = %d, wait SIGABRT\n", getpid());

    pause();
    puts("Second pause");
    pause();

    exit(EXIT_SUCCESS);
}